package com.zy.activity

import com.zy.zlog.ZLog.e
import com.zy.zlog.ZLog.d
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.View
import androidx.fragment.app.Fragment

open class BaseFragment : Fragment() {
    protected val mHandler = Handler(Looper.getMainLooper())
    protected var stepOne = 0f
    protected var widthPixels = 0f
    protected var heightPixels = 0f
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initDimen()
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(layout, container, false)
    }

    protected open val layout: Int
        protected get() = R.layout.fragment_base

    protected fun checkActivityDestroyed(): Boolean {
        if (activity == null || activity!!.isFinishing || activity!!.isDestroyed) {
            e(
                TAG,
                "getActivity() == null || getActivity().isFinishing() || getActivity().isDestroyed()"
            )
            return true
        }
        return false
    }

    private fun initDimen() {
        val displayMetrics = resources.displayMetrics
        widthPixels = displayMetrics.widthPixels.toFloat()
        heightPixels = displayMetrics.heightPixels.toFloat()
        val dp_16 = resources.getDimension(R.dimen.dp_16)
        d(TAG, "displayMetrics:$displayMetrics")
        d(TAG, "heightPixels:$heightPixels")
        d(TAG, "widthPixels:$widthPixels")
        d(TAG, "dp_8:$dp_16")
        stepOne = (widthPixels - dp_16) / 10
        d(TAG, "stepOne:$stepOne")
    }

    protected fun mainRunPost(long: Long, run: Runnable) {
        mHandler.postDelayed(run, long)
    }

    companion object {
        private const val TAG = "BaseFragment"
    }
}